//
//  TableFormStackCell+Layout.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/6/3.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit

extension TableFormStackCell {
    
    func layoutViews() {
        
        // 背景view
        bgView = genStackView().then{
            $0.alignment = .fill
        }
        contentView.addSubview(bgView)
        // 底线
        bottonLineView = UIView.init().then{
            $0.backgroundColor = UIColor(hex: "#f3f4f5")
        }
        contentView.addSubview(bottonLineView)
        
        // 约束
        bgView.snp.makeConstraints {
            $0.top.left.right.equalTo(contentView).offset(0)
            $0.bottom.equalTo(bottonLineView.snp_topMargin).offset(0)
            $0.height.equalTo(54)
        }
       
        bottonLineView.snp.makeConstraints {
            $0.left.right.bottom.equalTo(contentView).offset(0)
            $0.height.equalTo(1)
        }
        
        // 中间
        leftStackView = genStackView()
        rightStackView = genStackView()
        centerStackView = genStackView()
        
        // 左侧配置
        let lefts = configLabelAndImages(numof: 5, tag: 700, stack: &leftStackView)
        leftLBs = lefts.0
        leftImgs = lefts.1
        leftViews = lefts.2
    
        
        // 右侧配置
        let rights = configLabelAndImages(numof: 5, tag: 800, stack: &rightStackView)
        rightLBs = rights.0
        rightImgs = rights.1
        rightViews = rights.2
        
        
        // 中间配置
        
        // 中间左侧
        let centerLefts = configLabelAndImages(numof: 3, tag: 1700, stack: &centerStackView)
        centerLeftLBs = centerLefts.0
        centerLeftImgs = centerLefts.1
        centerLeftViews = centerLefts.2
        
        // 中间显示区域
        centerViews.removeAll()
        [createTF(1800), createLB(1801), createTV(1802), createBT(1803)]
            .forEach { v in
                centerViews.append(v)
            centerStackView.addArrangedSubview(v)
            }
        
        // 中间右侧
        let centerRights = configLabelAndImages(numof: 3, tag: 1900, stack: &centerStackView)
        centerRightLBs = centerRights.0
        centerRightImgs = centerRights.1
        centerRightViews = centerRights.2
        
 
        // 添加
        bgView.addArrangedSubview(leftStackView)
        bgView.addArrangedSubview(centerStackView)
        bgView.addArrangedSubview(rightStackView)
        
    }
    
    // MARK: - 创建组件
    func configLabelAndImages(numof num: Int, tag: Int, stack: inout UIStackView) -> ([UILabel], [UIImageView], [UIView]) {
        var views = [UIView]()
        var lbs = [UILabel]()
        var images = [UIImageView]()
        for i in 0...num {
            let view = UIView.init().then {
                $0.tag = tag + i
//                $0.backgroundColor = .random()
            }
            views.append(view)
            stack.addArrangedSubview(view)
            
            let lb = createLB(tag + 10000 + i)
            lbs.append(lb)
            stack.addArrangedSubview(lb)
            
            let imageV = createImgV(tag + 20000 + i)
            images.append(imageV)
            stack.addArrangedSubview(imageV)
        }
        
        let view = UIView.init().then {
            $0.tag = tag + num
        }
        views.append(view)
        stack.addArrangedSubview(view)
        
        return (lbs, images, views)
    }
    
    // MARK: - 默认 UIStackView 创建
    func genStackView() -> UIStackView {
        return UIStackView.init().then {
            $0.alignment = .center
            $0.axis = .horizontal
            $0.distribution = .fill
            $0.spacing = 4
//            $0.backgroundColor = .random()
        }
    }
    
    func createTV(_ tag: Int) -> UITextView {
        let tv = UITextView.init().then {
            $0.tag = tag
        }
        return tv
    }
    
    func createBT(_ tag: Int) -> UIButton {
        let bt = UIButton.init().then {
            $0.tag = tag
        }
        return bt
    }
    
    func createTF(_ tag: Int) -> UITextField {
        let tf = UITextField.init().then {
            $0.tag = tag
        }
        return tf
    }
    
    
    func createImgV(_ tag: Int) -> UIImageView {
        let imgV = UIImageView.init().then {
            $0.tag = tag
        }
        return imgV
    }
    
    func createLB(_ tag: Int) -> UILabel {
        let lb = UILabel.init().then {
            $0.text = ""
            $0.textColor = .darkText
            $0.tag = tag
        }
        return lb
    }
    
}
